package com.kanshu.ksgb.fastread.doudou.common.business.pay;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.alipay.sdk.app.PayTask;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver;
import com.kanshu.ksgb.fastread.doudou.common.util.JsonUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.ToastUtil;
import com.kanshu.ksgb.fastread.doudou.common.view.LoadingDialog;
import com.kanshu.ksgb.fastread.doudou.common.business.pay.bean.AliOrderBean;
import com.kanshu.ksgb.fastread.doudou.common.business.pay.bean.OrderRequestParams;
import com.kanshu.ksgb.fastread.doudou.common.business.pay.bean.PayResult;
import com.kanshu.ksgb.fastread.doudou.common.business.pay.event.PayActionEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;
import java.util.concurrent.Executors;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wcy on 2017\6\29 0029.
 */

public class AliPayHelper {
    private Activity mActivity;
    private boolean mPayFlg;
    private LoadingDialog mLoadingDialog;

    public AliPayHelper(Activity activity) {
        mActivity = activity;
        mLoadingDialog = new LoadingDialog(mActivity, "正在加载中");
    }

    public Activity getActivity() {
        return mActivity;
    }

    public void payAction(OrderRequestParams params) {
        if (mPayFlg) {
            return;
        }
        mPayFlg = true;
        mLoadingDialog.show();
        RetrofitHelper helper = RetrofitHelper.getInstance();
        Map<String, String> map = JsonUtils.jsonStrToMap(params.toJsonStr());
        helper.createService(PayService.class).getAliOrder(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<AliOrderBean>() {
            @Override
            public void onResponse(BaseResult<AliOrderBean> aliOrderBeanBaseResult, AliOrderBean aliOrderBean, Disposable disposable) {
                mPayFlg = false;
                mLoadingDialog.dismiss();
                if (aliOrderBean != null) {
                    pay(aliOrderBean.ali_pay);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                mPayFlg = false;
                mLoadingDialog.dismiss();
                ToastUtil.showMessage(mActivity.getResources().getString(R.string.error_back));
            }
        });
    }

    public void pay(String ali_pay) {
        if (ali_pay == null) {
            return;
        }
        final Handler mHandler = new Handler(Looper.getMainLooper());
        final String payInfo = ali_pay;
        Runnable payRunnable = () -> {
            PayTask alipay = new PayTask(getActivity());
            final String result = alipay.pay(payInfo, true);
            mHandler.post(() -> {
                PayResult payResult = new PayResult(result);
                String resultStatus = payResult.getResultStatus();
                PayActionEvent payActionEvent = new PayActionEvent();
                payActionEvent.payment = PayActionEvent.PAYMENT.ZHI_FU_BAO;
                if (TextUtils.equals(resultStatus, "9000")) {
                    payActionEvent.isSuccess = true;
                } else {
                    payActionEvent.isSuccess = false;
                }
                EventBus.getDefault().post(payActionEvent);
                //notifyServer(payResult);
            });
        };
        Executors.newSingleThreadExecutor().execute(payRunnable);
    }

}
